import cgi
import datetime
import wsgiref.handlers
import os

from models import *
from services.loanservice import LoanService
from services.bookservice import BookService
from services.userservice import UserService
from services.bookownershipservice import BookOwnershipService
from Session import Session

from google.appengine.ext.webapp import template
#from google.appengine.ext import db should never need to access the database directly. go through the services please
from google.appengine.api import users
from google.appengine.ext import webapp

class BookHandler(webapp.RequestHandler):
	def get(self):
		user = Session().getUser()
		books = BookOwnershipService().getBooksForOwner(user)
		template_values = {'books':books}
		path = os.path.join(os.path.dirname(__file__), 'templates/Books.html')
		self.response.out.write(template.render(path, template_values))
	def post(self):
		name = self.request.get('name')
		author = self.request.get('author')
		isbn = self.request.get('isbn')
		user = Session().getUser()
		BookOwnershipService().createBookOwnership(user,name,author,isbn)
		self.redirect('/books')

application = webapp.WSGIApplication([
  ('/books', BookHandler),
], debug=True)

def main():
  wsgiref.handlers.CGIHandler().run(application)


if __name__ == '__main__':
  main()